package com.randy.fundation;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageUitls {
    public static class ImageSize {
     public    int width;
        public  int height;

        public ImageSize(int width, int height) {
            this.width = width;
            this.height = height;
        }
    }

    public static ImageSize getImageSize(String path) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
            int imageHeight = options.outHeight;
            int imageWidth  = options.outWidth;

            return new ImageSize(imageWidth, imageHeight);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ImageSize(0, 0);
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    public static Bitmap scaleImage(Bitmap originImage, int width, int height) {
        Bitmap background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        float originalWidth  = originImage.getWidth();
        float originalHeight = originImage.getHeight();

        if (originalWidth == width && originalHeight == height){
            return originImage;
        }

        Canvas canvas = new Canvas(background);

        float scaleX       = width / originalWidth;
        float scaleY       = height / originalHeight;
        float scale        = Math.min(scaleX, scaleY);
        float xTranslation = (width - originalWidth * scale) / 2.0f;
        float yTranslation = (height - originalHeight * scale) / 2.0f;

        Matrix transformation = new Matrix();
        transformation.postTranslate(xTranslation, yTranslation);
        transformation.preScale(scale, scale);

        Paint paint = new Paint();
        paint.setFilterBitmap(true);

        // canvas.drawARGB(0, 225, 225, 255);
        canvas.drawBitmap(originImage, transformation, paint);

        return background;
    }
}
